<?php 

class bc_ctr extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
      
		  $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('Convert_ctr');// elle convert les chiffres en lettre arab
    }
	  
	 function index()
	{
		
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "bc_ctr/index");
		  $config["total_rows"] =$this->bc_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->bc_mdl->get_all($config["per_page"], $page);
	    $data["wilaya"] = $this->bc_mdl->get_wilaya();
	    $data["results_recherche"] = $this->bc_mdl->get_all_per_nom();
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة وصولات الطلب ';
		  $this->load->view('bc_vew',$data);  // send the result from the model to the view 
		  
	
	
	}
	
	
	public function find_bc_per_date()
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$j1=$this->input->post('jour1');
		$j2=$this->input->post('jour2');
		$m1=$this->input->post('mois1');
		$m2=$this->input->post('mois2');
	   
	   
	   
      
	    $data["results_recherche"] = $this->bc_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
      $data["wilaya"] = $this->bc_mdl->get_wilaya();
	   $data["results"]= $this->bc_mdl->get_per_date($j1,$j2,$m1,$m2);
	    $data['titre']=': وصولات الطلب بالتاريخ <br> ';
		 $data['links']=' ';
	    $this->load->view("bc_vew",$data);	
	   
    }
	
	public function find_bc_per_wilaya()
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$num_wilaya=$this->input->post('wilaya');
		
	   
	   
	   
      
	    $data["results_recherche"] = $this->bc_mdl->get_all_per_nom();//elle appaort tt les fournisseur pour rechercher leur convention
       $data["wilaya"] = $this->bc_mdl->get_wilaya();
	   $data["results"]= $this->bc_mdl->get_per_wilaya($num_wilaya);
	    $data['titre']=' وصولات الطلب الخاصة بالولاية : '.$num_wilaya.'<br> ';
		 $data['links']=' ';
	    $this->load->view("bc_vew",$data);	
	   
    }
	
	public function get_bc_per_article2($x1)
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['nbr-enreg']= $this->bc_mdl->record_count($x1);
	   $tab['premier_appelle']=TRUE;
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_bc_per_article3();
		
	
	
    }
	
	public function get_bc_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val
  $tab['premier_appelle']=TRUE;
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("bc_crt/get_bc_per_article3");
		  $config["total_rows"] =$this->bc_mdl->record_count($x1);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 //
       $this->pagination->initialize($config);
	   
	    $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		   $this->session->set_userdata($tab); // mettre $tab dans une var globale
		   }
	else	 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
	
		
       $data["results"] = $this->bc_mdl->get_all2($config["per_page"], $page ,$x1);
	   $data["results_recherche"] = $this->bc_mdl->get_all_recherche($x1);
        $data["links"] = $this->pagination->create_links();
		$data["wilaya"] = $this->bc_mdl->get_wilaya();
			$tt=$data["results"];
		if($tt){
			$nom_article=$tt[0]->article;
			$data['titre']='وصولات الطلب  الخاصة بالمادة '.$nom_article;
		}else{
			$art = $this->charticle_mdl->get_nom_article($x1);
			$data['titre']=': لا يوجد وصولات الطلب خاصة بالمادة  <br>'.$art[0]->nom ;
		}
		
		 $this->load->view("bc_vew",$data);			
	
	
    }
	
		public function get_detaille_bc($id)
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$result['les_bc'] = $this->bc_mdl->get_detaille($id);
	
		$tmp=$result['les_bc'];
		$montant=0;
		   foreach($tmp as $data) 
{ 
        $montant=$montant +($data->quantite * $data->bc_produit_pu);
	   
		
	
}
}
	public function get_detaille_bc2($id,$rmq)
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('produit_mdl'); // load  
		
		$result['les_bc'] = $this->bc_mdl->get_detaille($id);
	    $result['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$result['produits'] = $this->produit_mdl->get_dessignation();
		$result['rmq']=$rmq;
		$tmp=$result['les_bc'];
		$montant=0;
		if(array_key_exists('quantite',$tmp[0])){
		   foreach($tmp as $data) { 
           $montant=$montant +($data->quantite * $data->bc_produit_pu)+(($data->quantite * $data->bc_produit_pu)*$data->tva/100);
	        }
			
		}  
		  $montant=number_format($montant, 2, '.', '');
		     $lettre=$this->convert_ctr->convert_number($montant);
			$result['lettre']=$lettre;
            $result['montant'] =$montant;
			$this->load->view('bc_detaille_vew2',$result); 
    }
public function find_name()
    {
		$this->load->model('bc_mdl'); // load the file welcome_mdl.php
		$v=$this->input->post('nom');
		$data["results"] = $this->bc_mdl->get_per_nom_fourn($v);
		 $data["results_recherche"] = $this->bc_mdl->get_all_per_nom();
		 
		   $config = array();
         $config["base_url"] = base_url("bc_ctr/find_name");
		  $config["total_rows"] =$this->bc_mdl->count_per_nom_fourn($v);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
         $this->pagination->initialize($config);
	     
		  $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
	
          $data["links"] = $this->pagination->create_links();
		  $data["wilaya"] = $this->bc_mdl->get_wilaya();
	       $data['titre']=': وصولات الطلب الخاصة بالمورد <br> '.$v;
		 $this->load->view('bc_vew',$data);  // send the result from the model to the view 
		}
		
 public function test($i) {
		
	    echo '<div class="pure-u-1 pure-u-md-1-3 ">
                <label for="numero"> dessignation </label>
				<input id="dessignation'.$i.'"  class="ltr" type="text" value="isis" name="numero" >
            </div>
			<div class="pure-u-1 pure-u-md-1-3 ">
                <label for="numero"> prix u </label>
				<input class="ltr" id="numer" name="numero" type="text" value=" ">
            </div>
			<div class="pure-u-1 pure-u-md-1-3 ">
                <label for="numero"> quantite </label>
				<input class="ltr" id="numer" name="numero" type="text" value=" ">
            </div>
			<div class="pure-u-1 pure-u-md-1-3 ">
                <label for="numero"> prix </label>
				<input class="ltr" id="numer" name="numero" type="text" value=" ">
            </div>
			
		
		';
       
    }

public function ajout_bc($m)
    {
		$this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('charticle_mdl'); // load 
		
	    $result['fournisseurs'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
	    $result['charticles'] = $this->charticle_mdl->get_all();   // get all  for autocomplete
		$result['message']=$m;
		if(is_array($m)){
			
	    if(array_key_exists('0',$m)){
			$result['nom_fourn']=$m[0]->nom;
		}
		}else $result['nom_fourn']=NULL;
		$this->load->view('bc_ajout_vew',$result); 
    
    }	
		   
  public function supprimer_bc($id_bc)
    {
		
		$this->load->model('bc_mdl'); // load 
		$tmp=$this->bc_mdl->supprimer_bc($id_bc);
	   
	 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
	
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "bc_ctr/index");
		  $config["total_rows"] =$this->bc_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = 0;
       $data["results"] = $this->bc_mdl->get_all($config["per_page"], $page);
	    $data["results_recherche"] = $this->bc_mdl->get_all_per_nom();
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة وصولات الطلب الكلية';
		  $this->load->view('bc_vew',$data);  // send the result from the model to the view 
    }		
	
public function ajout_produit($id,$id_fournisseur)
    {
		
		$this->load->model('produit_mdl'); // load 
		$result['produit'] = $this->produit_mdl->get_dessignation();
		$result['id_bc']=$id;
		$result['id_fournisseur']=$id_fournisseur;
		$this->load->view('produit_ajout_vew',$result); 
		
    }		

public function valider_ajout_prod($id,$id_fournisseur)
    {
		$dessignation= $this->input->post('dessignation');
		$prix= $this->input->post('prix');
		$quantite= $this->input->post('quantite');
		$this->load->model('produit_mdl'); // load 
		$produit = $this->produit_mdl->get_prix($dessignation);
		//on va verifie le prix
		if($produit){ //test si le produit existe deja
		$prix_originale=$produit[0]->prix_u;
		if($prix<=$prix_originale){ //test si le produit est chere
		$tab['dessignation']=$dessignation;
		$tab['id_bc']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$tab['prix_orig']=$prix_originale;
		$result = $this->produit_mdl->ajout_produit_bc($tab);
		// $this->get_detaille_bc2($id);  // send the result from the model to the view 
		 $this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}else{
			// prix non raissonable
			$tab['dessignation']=$dessignation;
			$tab['prix']=$prix;
			$tab['prix_original']=$prix_originale;
			$tab['quantite']=$quantite;
			$tab['id_bc']=$id;
			$tab['id_fournisseur']=$id_fournisseur;
			$this->load->view('produit_ajout_chere_vew',$tab); 
			
		}
		}else{// on cree nouveau produit
		$this->produit_mdl->ajout_nouveau_produit($dessignation,$prix);
		$tab['dessignation']=$dessignation;
		$tab['id_bc']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$result = $this->produit_mdl->ajout_produit_bc($tab);
		// $this->get_detaille_bc2($id); 
	     $this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}
		$d=0;
    }
	
	
	
	public function verifie_seuill_fournisseur($id_fournisseur,$id_bc){
		$this->load->model('bc_mdl'); // load 
	    $this->load->library('session');
        $seuill=$this->session->userdata('seuill');

		$info_bc=$this->bc_mdl->get_bc_produit($id_bc);
		$prix_bc=0;
		if($info_bc){
			foreach($info_bc as $tmp){
				$prix_bc=$prix_bc+($tmp->bc_produit_pu * $tmp->quantite)+(($tmp->bc_produit_pu * $tmp->quantite)*$tmp->tva/100);
			}
		}
		$prix_bc=number_format($prix_bc, 2, '.', '');
		$this->bc_mdl->set_montant($id_bc,$prix_bc);
		if($prix_bc<$seuill){
		$tab_bc=$this->bc_mdl->get_detaille_mini($id_bc);	
		$id_art=$tab_bc[0]->id_article_fk;
		$info=$this->bc_mdl->verifie_seuill_regulier($id_fournisseur,$id_art);
		$prix=0;
		$fournisseur= $this->input->post('engagement');
		foreach($info as $data) 
{
// on calcule le montant fe tout les bon non regularise
$prix=$prix+($data->bc_produit_pu * $data->quantite)+(($data->bc_produit_pu * $data->quantite)*$data->tva/100);
}
if($prix<500000){
	$rest=500000-$prix;
	//echo "<script> alert(\"il vous rest $rest \")</script>";
	$rmq=0;
	$this->get_detaille_bc2($id_bc,$rmq);
}else{ // convention regularisation
    $rmq=1;
	echo "<script> alert(\" لقد تجاوزت الوصل  $prix \")</script>";
	$this->get_detaille_bc2($id_bc,$rmq);
}
		$v=0;
		}else{
			$rmq=0;
			echo "<script> alert(\" لقد تجاوز الوصل  الحد الاقصى يجب تغيير محتوياته \")</script>";
			$this->get_detaille_bc2($id_bc,$rmq);
		}
	
}

    public function mise_a_jour_bc($id_bc,$id_article){
		$this->load->model('bc_mdl'); // load 
		$this->load->model('engagement_mdl'); // load 
		// on engage un bc
		//$num_enga=$this->engagement_mdl->get_engagement_numero($id_article);
		$num_enga=$this->engagement_mdl->get_engagement_prec($id_article);
		
		if(!$num_enga){
			 echo "<script> alert(\" non تمت عملية الالتزام بنجاح\")</script>";
			 $tab_bc=$this->bc_mdl->get_detaille_mini($id_bc);
		$id_fourn=$tab_bc[0]->id_fournisseur_fk;
			 $this->verifie_seuill_fournisseur($id_fourn,$id_bc);
		}else{
			if($num_enga[0]->nature==1){// si nature est depense
				$montant_prec=$num_enga[0]->montant_precedent-$num_enga[0]->montant_engagement;
			}else{
				$montant_prec=$num_enga[0]->montant_engagement+$num_enga[0]->montant_precedent;
			}
			
			$tab_bc=$this->bc_mdl->get_detaille_mini($id_bc);
			$montant=$tab_bc[0]->montant;
			$numero=$num_enga[0]->numero+1;
			$nature=1;
			$today = getdate();
			$time=$today[0];
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			$this->engagement_mdl->ajout_engagement3($id_article,$nature,$date,$numero,$time,$montant,$montant_prec);
			$num_enga=$this->engagement_mdl->get_engagement_numero($id_article);
			$id_engagement=$num_enga[0]->id_engagement;
			
		$id_fourn=$tab_bc[0]->id_fournisseur_fk;
		$this->bc_mdl->engager_bc($id_bc,$id_engagement);
		$rmq=0;
		//$engagement= $this->input->post('engagement');
        echo "<script> alert(\" تمت عملية الالتزام بنجاح\")</script>";
	//$this->get_detaille_bc2($id_bc,$rmq);
	
       $this->verifie_seuill_fournisseur($id_fourn,$id_bc);
		}
		
		
	}
	
	 public function modifier_bc($id_bc){
		$this->load->model('bc_mdl'); // load 
		
		// on modifier un bc
		
		$date= $this->input->post('date');
		$numero= $this->input->post('numero');
		$bon=TRUE;
		
		if (!(preg_match('/\\d{4}\\-\\d{2}-\\d{2}/', $date))){
			echo "<script> alert(\" هناك خطأ في التاريخ\")</script>";
			$bon=FALSE;
		}
		
		if($numero){
			$tab_numero=$this->bc_mdl->verifie_numero($numero);
			if($tab_numero){
				if($tab_numero[0]->id_bc!=$id_bc){
					echo "<script> alert(\" يوجد هذا الرقم\")</script>";
			        $bon=FALSE;
				}
				 
			}
		}else{
			$bon=FALSE;
		}
		
		if($bon){
			$this->bc_mdl->modifier_bc($id_bc,$numero,$date);
		
		}
			$rmq=0;
			$this->get_detaille_bc2($id_bc,$rmq);
		
		

	}

	public function ajout_mission()
    {
		$nom= $this->input->post('nom');
		$grade= $this->input->post('grade');
		$date= $this->input->post('date');
		$transport= $this->input->post('transport');
		$normale= $this->input->post('normale');
		$nuit= $this->input->post('nuit');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('bc_mdl'); // load 
		
		$this->bc_mdl->ajout_mission($nom,$grade,$date,$transport,$normale,$nuit);
		
		
    }
	
	public function valider_ajout_bc()
    {
		$fournisseur= $this->input->post('fournisseur');
		$article= $this->input->post('article');
		$tva= $this->input->post('tva');
		$numero= $this->input->post('numero');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('bc_mdl'); // load 
		$this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('charticle_mdl'); // load 
		
		$mission=FALSE;
		if($article){// si nom article entree
			$id_article = $this->charticle_mdl->get_id_article($article);
			if(array_key_exists(0,$id_article)){
					$id_art=$id_article[0]->id_article;
					if($id_art==1){
					$mission=true;	
					}
					}
			else $erreurs['article']=true;
		}else{
			$erreurs['article']=true;
		}
		
		if($fournisseur){// si nom fournisseur entree
			$id_fournisseur = $this->fournisseur_mdl2->get_id($fournisseur);
			if(array_key_exists(0,$id_fournisseur))	$id_fourn=$id_fournisseur[0]->id_fournisseur;
			else $erreurs['fournisseur']=true;
			
		}else{
			$erreurs['fournisseur']=true;
		}
		
		if($mission){
			$this->load->view('mission_ajout_vew'); 
		}
		elseif(empty($erreurs)){
			$rmq=0;
			$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			$numero_bon=TRUE;
		    if($numero){
				$tab_numero=$this->bc_mdl->verifie_numero($numero);
				if($tab_numero)$numero_bon=FALSE;
				else $numero_bon=true;
			}else{
				$tmp_num=$this->bc_mdl->get_numero();
				if($tmp_num)$numero=$tmp_num[0]->numero+1;
				else $numero=1;
			}
			if($numero_bon){
				$id_engagement=NULL;
			$this->bc_mdl->ajout_bc($id_fourn,$id_engagement,$id_art,$date,$numero,$tva);
			$new_id=$this->bc_mdl->get_bc_nouveau_id();
				$id=$new_id[0]->id_bc;
			$this->verifie_seuill_fournisseur($id_fourn,$id);
			}else{
				echo('<script> alert("numero existe");</script>');
				$msg='erreur champ';
			$this->ajout_bc($msg);
			}
		}else{
			echo('<script> alert("هناك خطأ في تعبئة الوصل");</script>');
			$msg='erreur champ';
			$this->ajout_bc($msg);
		}
		
    }

    public function ajout_annex_bc()
    {
		$fournisseur= $this->input->post('fournisseur');
		$article= $this->input->post('article');
		//$engagement= $this->input->post('engagement');
		$numero= $this->input->post('numero');
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('bc_mdl'); // load 
		$this->load->model('fournisseur_mdl2'); // load 
		$this->load->model('charticle_mdl'); // load 
		
	
		
    }
	   public function valider_ajout_prod_chere($id,$id_fournisseur)
    {
		$dessignation= $this->input->post('dessignation');
		$prix= $this->input->post('prix');
		$quantite= $this->input->post('quantite');
		$this->load->model('produit_mdl'); // load 
		
	
		$tab['dessignation']=$dessignation;
		$tab['id_bc']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		
		$result = $this->produit_mdl->ajout_produit_bc($tab);
		$this->verifie_seuill_fournisseur($id_fournisseur,$id);  // send the result from the model to the view 
		
		}		
		
		 public function modifier_prod($i,$id,$id_bc_prod,$id_fournisseur)
    { 
	    $rmq=0; // var pr le besoin de convention de regularisation
		$dessignation= $this->input->post('dessignation'.$i);
		$prix= $this->input->post('prix'.$i);
		$quantite= $this->input->post('quantite'.$i);
		$pu= $this->input->post('pu'.$i);
		
		$this->load->model('produit_mdl'); // load 
		
		$tab['dessignation']=$dessignation;
		$tab['id_bc']=$id;
		$tab['prix']=$prix;
		$tab['quantite']=$quantite;	
		$tab['pu']=$pu;	
		if($dessignation==""){ // si le champs est vide on supprime le prod
			$result = $this->produit_mdl->supprime_produit_bc($id_bc_prod);
			//$this->get_detaille_bc2($id,$rmq);
			$this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}else{ // sinon on modifier le prod
		    $produit = $this->produit_mdl->get_prix($dessignation);
		//on va verifie le prix
		    if(!$produit){ //test si le produit existe deja
		    $this->produit_mdl->ajout_nouveau_produit($dessignation,$prix);
			$produit = $this->produit_mdl->get_prix($dessignation);
			}
			$tab['id_prod']=$produit[0]->id_produit;
			$result = $this->produit_mdl->modifier_produit_bc($id_bc_prod,$tab);
			//$this->get_detaille_bc2($id,$rmq);
			$this->verifie_seuill_fournisseur($id_fournisseur,$id);
		}
	
		
		}	
		
		 public function supprime_prod($id_bc_prod,$id,$id_fournisseur)
        { 		
		
		$this->load->model('produit_mdl'); // load 
		$result = $this->produit_mdl->supprime_produit_bc($id_bc_prod);
		$this->verifie_seuill_fournisseur($id_fournisseur,$id);	
		
		}
		   
		    public function payer_bc($id_bc,$id_fournisseur,$montant)
        { 		
		$this->load->model('bc_mdl'); // load 
		$this->load->model('mondat_mdl'); // load 
		$this->load->model('facture_mdl'); // load 
		
		$num_cheque= $this->input->post('num_cheque');
		$num_mondat= $this->input->post('num_mondat');
		$num_facture= $this->input->post('num_facture');
		$today = getdate();
		$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		$id_mond=$this->mondat_mdl->get_num_mondat();
		if($id_mond){
			$id_mond=$id_mond[0]->id_mandat+1;
		}else{
			$id_mond=1;
		}
		
		$bool=$this->facture_mdl->verifie_facture_numero($num_facture,$id_fournisseur);
		if($bool){//test si la facture existe
		$this->mondat_mdl->ajout_new_mondat($num_cheque,$montant,$date,$num_mondat);
		$this->facture_mdl->attacher_facture_mondat($num_facture,$id_fournisseur,$id_mond);
		$result = $this->mondat_mdl->get_num_mondat();
		$id_mondat=$result[0]->id_mandat;
		$this->bc_mdl->attacher_bc_mondat($id_bc,$id_mondat);
		
		}else{
			echo('<script>window.alert("لا توجد فاتورة بهذا الرقم");</script>');
		}
		$this->verifie_seuill_fournisseur($id_fournisseur,$id_bc);	
		}
		   
		   
		     public function imprimer($id_bc,$id_fournisseur)
        { 		
		$this->load->model('bc_mdl'); // load 
		
		
		$result['les_bc'] = $this->bc_mdl->get_detaille($id_bc);
		$tmp=$result['les_bc'];
		$montant=0;
		//if(array_key_exists('quantite',$tmp[0])){
		   foreach($tmp as $data) { 
           $montant=$montant +($data->quantite * $data->bc_produit_pu);
	        }
			
	//	}  
		     $lettre=$this->convert_ctr->convert_number($montant);
			$result['lettre']=$lettre;
            $result['montant'] =$montant;
		    $this->load->view('bc_detaille_imprimer_vew',$result); 
		
		}	
		
		public function ajout_fournisseur_pour_bc($tab,$errors){
		
		$this->load->model('fournisseur_mdl2');
		$this->load->model('activite_mdl');
		$this->load->model('banque_mdl');
		
		
		$data2['rec'] = $this->fournisseur_mdl2->get_all();   // get all  for autocomplete
		$data3['rec'] = $this->activite_mdl->get_all();  //get all activity list for autocomplete 
		$data4['rec'] = $this->banque_mdl->get_all_banques(); // get the bank details for the fournisseur

		$r['liste_fourn']    = $data2;
		$r['liste_activite'] = $data3;
		$r['liste_banques'] = $data4;
		$r['errors']=$errors;
        $r['tab']=$tab;
		
		$this->load->view('fournisseur_ajout_vew2',$r); 
	}
		   
		 public function valider_ajout_fournisseur_pour_bc()
	{
		//$this->load->model('banque_mdl');
		
		$errors = array();
		// check for data concistency
		$phone= $this->input->post('phone');
		//$mobile= $this->input->post('mobile');
		$rc= $this->input->post('rc');
		$rib= $this->input->post('rib');
		$email= $this->input->post('email'); 
		$numero= $this->input->post('numero');
		$fax= $this->input->post('fax');
		$num_fiscale= $this->input->post('num_fiscale'); 
		$nom= $this->input->post('nom'); 
		$address= $this->input->post('address'); 
		$type_activit= $this->input->post('type_activit');
		$wilaya= $this->input->post('wilaya'); 
		$banque= $this->input->post('banque'); 
		$adress_banque= $this->input->post('adress_banque'); 
		
		$this->load->model('activite_mdl');
		$data = $this->activite_mdl->get_activite_per_nom($type_activit);  //get all activity list for autocomplete 
		if($data) $id_activite=$data[0]->id_activite;
		else $errors['type_activit'] = TRUE;
		
		/*if($banque){
			
		$data_banque = $this->banque_mdl->get_id_banque($banque);  
			if($data_banque){// la banque existe deja
			$id_banque=$data_banque[0]->id_banque;
		}else{ // si la banque ne existe pas oncree une
			$la_banque = $this->banque_mdl->ajout_banques($banque); 
			$id_banque=$la_banque[0]->id_banque;
		}
		}else  $errors['banque'] = TRUE;
		*/
		
		if (! $nom)
		{
			 $errors['nom'] = TRUE;
		}
		
		if (! $wilaya)
		{
			 $errors['wilaya'] = TRUE;
		}
		
		if (! $address)
		{
			 $errors['address'] = TRUE;
		}
		
		if (! $phone)
		{
			 $errors['phone'] = TRUE;
		}
		
		if (! $fax)
		{
			 $errors['fax'] = TRUE;
		}
		
		if (! $adress_banque)
		{
			 $errors['adress_banque'] = TRUE;
		}
		
		if (!$num_fiscale)
		{
			 $errors['num_fiscale'] = TRUE;
		}
		/*if (!(preg_match('/\\(0(5|6|7)\\d{2}\\)-\\d{2}-\\d{2}-\\d{2}/', $mobile)))
		{
			 $errors['mobile'] = TRUE;
		}*/
		if (! $rc)
		{
			 $errors['rc'] = TRUE;
		}
		if (! $rib)
		{
			 $errors['rib'] = TRUE;
		}
		if($email != FALSE) // if email was typed check it's syntax
		{
			if (!(preg_match('/[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}/', $email)))
			{
				 $errors['email'] = TRUE;
			}
		}
		
		/// if no errors do update
		if (empty($errors)) { 
		    $this->load->model('fournisseur_mdl2');
			
			if($numero){// si le numero fournisseur est entree
				
			$existe_numero=$this->fournisseur_mdl2->test_numero($numero);
			// on test si le numero existe
			if($existe_numero) $errors['numero'] = TRUE;
			 // get  one fournisseur per id
			
			}else{// si numero non entree on cree un
				$tab_num=$this->fournisseur_mdl2->get_new_number();
				if($tab_num){
					$numero=$tab_num[0]->numero+1;
				}else $numero=1;
			}
			$tmp=$this->fournisseur_mdl2->ajout_fournisseur_new($nom,$phone,$fax,$rc,$rib,$email,$address,$id_activite,$numero,$wilaya,$num_fiscale,$banque,$adress_banque);
			$id=$tmp[0]->id_fournisseur;
		//	$this->banque_mdl->ajout_banques_fournisseur($id_banque,$id);
			
			$this->ajout_bc($tmp);
		
		}
		else{
			// do not update
			echo"<script> alert(\"هناك حقل ادخال غير مطابق\")</script>"; 
			$tab['nom']=$nom;
			$tab['phone']=$phone;
			$tab['numero']=$numero;
			$tab['fax']=$fax;
			$tab['address']=$address;
			$tab['rc']=$rc;
			$tab['num_fiscale']=$num_fiscale;
			$tab['rib']=$rib;
			$tab['type_activit']=$type_activit;
			$tab['email']=$email;
			$tab['wilaya']=$wilaya;
			$tab['banque']=$banque;
			$this->ajout_fournisseur_pour_bc($tab,$errors);
		}
		$vv=1; 
	}		
			
			  public function ajout_convention_regularise($id_bc,$id_fournisseur){
		$this->load->model('convention_mdl');
		$this->load->model('bc_mdl');
		
		$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
		
		$tab_numero=$this->convention_mdl->get_numero();
		if($tab_numero){
			$numero=$tab_numero[0]->numero+1;
		}else $numero=1;
		$tmp=$this->convention_mdl->ajout_convention($id_fournisseur,$date,$numero);// on cree une convention et recupere id convention
		$id_convention=$tmp[0]->id_convetion;
		$tab=$this->bc_mdl->get_detaille_mini($id_bc);
		$id_article=$tab[0]->id_article_fk;
		$bcs=$this->bc_mdl->get_bc_pour_regule($id_fournisseur,$id_article);
		$montant_conv=0;
		foreach($bcs as $info){
			$this->bc_mdl->regularise_bc($info->id_bc,$id_convention);
			$temp=$this->bc_mdl->get_detaille_mini($info->id_bc);
			$montant_conv=$montant_conv+$temp[0]->montant;
		}
		//echo "<script> alert(\" convention ajoute avec succes \")</script>";
		$this->convention_mdl->set_montant($id_convention,$montant_conv);
		$rmq=0;
		$regularise=1;
		$this->detaille_convention_regularise($id_convention,$rmq);
	$g=0;
	}
	
	 public function detaille_convention_regularise($id_convention,$msg){
	
		$this->load->model('convention_mdl');		
		$tmp=$this->convention_mdl->get_convention_detaille($id_convention);
		$tmp2=$this->convention_mdl->get_convention_montant($id_convention);
		$bcs=$this->convention_mdl->get_convention_bcs($id_convention);
	
		$montant=0;
		foreach($tmp2 as $info){
			$montant=$montant+($info->quantite*$info->bc_produit_pu);
		}
		$tab=NULL;
		foreach($bcs as $datas){
			$cle=$datas->id_bc;
			$tmp2=$this->convention_mdl->get_prix_prod_bcs($cle);
			$mont=0;
			foreach($tmp2 as $info){
				$mont=$mont +($info->bc_produit_pu* $info->quantite);
			}
			$tab[$cle]=$mont;
		}
		$lettre=$this->convert_ctr->convert_number($montant);
		$data['result']=$tmp;
		$data['lettre']=$lettre;
		$data['montant']=$montant;
		$data['bcs']=$bcs;
		$data['montant_bcs']=$tab;
		$data['msg']=$msg;
		 $this->load->view('convention_regularise_detaille_vew',$data); 
	}
}
